package xian.woniuxy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import xian.woniuxy.domain.ResultVo;
import xian.woniuxy.entity.User;
import xian.woniuxy.mapper.custom.CustomUserMapper;
import xian.woniuxy.service.AuthService;
import xian.woniuxy.service.RoleService;

import javax.servlet.http.HttpSession;

/**
 * @author gao
 * @date 2023/07/05 15:42:07
 */
@RestController
public class AuthController {
    @Autowired
    private AuthService authService;

    @PostMapping("auth/login")
    public ResultVo login(@RequestBody User user, HttpSession session) {
        User loginUser = authService.login(user.getUsername(), user.getPassword());
        // 如果流程能走到这里，说明登录成功了， 因为如果登录失败，那么上面的login方法就会抛出异常，流程就无法到这里!
        session.setAttribute("loginUser", loginUser);
        return ResultVo.success(loginUser);
    }

    @PostMapping("auth/logout")
    public ResultVo login(HttpSession session) {
        session.invalidate();
        return ResultVo.success();
    }
}
